package com.nycreativity.elecreativity.controller


import com.nycreativity.elecreativity.aop.ValidateAspect
import com.nycreativity.elecreativity.entity.ResultData
import com.nycreativity.elecreativity.entity.cons.CommonURLConstants
import com.nycreativity.elecreativity.entity.dto.GoodsSpecificationDTO
import com.nycreativity.elecreativity.entity.dto.SkuSpecificationDTO
import com.nycreativity.elecreativity.entity.param.sku.SkuAddParam
import com.nycreativity.elecreativity.entity.param.sku.SkuQueryParam
import com.nycreativity.elecreativity.entity.param.sku.SkuUpdateParam
import com.nycreativity.elecreativity.entity.po.Sku
import com.nycreativity.elecreativity.entity.vo.PageDataVO
import com.nycreativity.elecreativity.entity.vo.goods.SkuOfGoodsVO
import com.nycreativity.elecreativity.entity.vo.sku.SkuVO
import com.nycreativity.elecreativity.service.SkuService
import com.nycreativity.elecreativity.util.PageUtil
import io.swagger.annotations.Api
import io.swagger.annotations.ApiImplicitParam
import io.swagger.annotations.ApiImplicitParams
import io.swagger.annotations.ApiOperation
import org.springframework.validation.BindingResult
import org.springframework.web.bind.annotation.*
import org.springframework.web.multipart.MultipartFile

import javax.annotation.Resource
import javax.validation.Valid

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sxt && ttxxi
 * @since 2021-03-22
 */
@RestController
@RequestMapping(CommonURLConstants.SKU_PREFIX)
@Api(tags = ["商品SKU相关接口"])
class SkuController {

    @Resource
    lateinit var skuService: SkuService

    @GetMapping
    @ApiOperation("根据id获取sku")
    fun getById(id: Long): ResultData<Sku> {
        val sku = skuService.getById(id) ?: return ResultData.FAILED("获取失败")
        return ResultData.SUCCESS(data = sku)
    }

    @PostMapping
    @ApiOperation("（在创建goods后）新增sku，此时设置图片、库存等信息")
    @ValidateAspect.ParamValidate
    fun createSku(
        @RequestParam("files") files: Array<MultipartFile>,
        @Valid skuAddParam: SkuAddParam,
        bindingResult: BindingResult
    ): ResultData<Any> {
        val res = skuService.addSku(files, skuAddParam)
        return if (res) {
            ResultData.SUCCESS()
        } else {
            ResultData.FAILED("添加失败，请检查格式")
        }
    }

    @GetMapping("/page")
    @ApiOperation("根据条件分页查找")
    @ValidateAspect.ParamValidate
    fun getPageByCondition(
        @Valid skuQueryParam: SkuQueryParam,
        bindingResult: BindingResult
    ): ResultData<PageDataVO<SkuVO>> {
        return ResultData.SUCCESS(data = PageUtil.convert(skuService.queryPageByCondition(skuQueryParam)))
    }

    @GetMapping("/goodsIdAndSpecification")
    @ApiOperation("根据商品id和规格查找,返回列表")
    @ApiImplicitParams(
        ApiImplicitParam(name = "goodsId", value = "商品id，注意不要混淆", required = true),
        ApiImplicitParam(name = "size", value = "尺寸(可空)"),
        ApiImplicitParam(name = "color", value = "颜色，精确查找(可空)")
    )
    fun getByCondition(@RequestParam goodsId: Long, skuSpecificationDTO: SkuSpecificationDTO): ResultData<List<SkuVO>> {
        val skuVO = skuService.queryByGoodsIdAndSpecification(goodsId, skuSpecificationDTO)
        return ResultData.SUCCESS(data = skuVO)
    }

    @PutMapping
    @ApiOperation("根据id修改sku")
    @ValidateAspect.ParamValidate
    fun updateById(@RequestBody @Valid skuUpdateParam: SkuUpdateParam, bindingResult: BindingResult): ResultData<Any> {
        skuService.updateById(skuUpdateParam)
        return ResultData.SUCCESS()
    }

    @PostMapping("/image")
    @ApiOperation("上传新的图片")
    fun uploadImage(@RequestParam("files") files: Array<MultipartFile>, id: Long): ResultData<Any> {
        val res = skuService.uploadNewImage(id, files)
        return if (res) {
            ResultData.SUCCESS()
        } else {
            ResultData.FAILED("id为空，上传图片失败")
        }
    }

    @DeleteMapping("/image")
    @ApiOperation("根据图片索引删除sku的图片")
    @ApiImplicitParams(
        ApiImplicitParam(name = "indexArray", value = "图片索引列表。（图片索引：图片字符串中用逗号分隔后，该图片所在的位置）")
    )
    fun deleteImage(id: Long, indexArray: IntArray): ResultData<Any> {
        skuService.deleteImage(id, indexArray)
        return ResultData.SUCCESS()
    }

    @DeleteMapping
    @ApiOperation("根据id删除sku")
    fun deleteById(id: Long): ResultData<Any> {
        skuService.deleteById(id)
        return ResultData.SUCCESS()
    }

    @GetMapping("specification/{goodsId}")
    @ApiOperation("根据商品id查询其对应的规格sku信息")
    fun getGoodsSpecification(@PathVariable("goodsId") goodsId: Long): ResultData<List<SkuOfGoodsVO>> {
        return ResultData(data = skuService.queryGoodsSpecification(goodsId))
    }


}

